package tech.abing.spider.task.pageprocessor.sac;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.sac.SecComEntity;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class SecuritiesCompanyInfoProcessor implements PageProcessor {

	private static final String TOP_URL = "http://jg.sac.net.cn/pages/publicity/indicatorsFrame.html";

	private static Logger logger = Logger.getLogger(SecuritiesCompanyInfoProcessor.class);
	private static final String SEARCH_URL = "http://jg.sac.net.cn/pages/publicity/resource!search.action";
	private static AtomicInteger atomicInteger = new AtomicInteger(0);
	
	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		// TODO Auto-generated method stub
		String json = page.getContent();
		JSONArray jsonArray = JSONArray.fromObject(json);
		if(jsonArray.size()>0){
			JSONObject scObj = jsonArray.getJSONObject(0);
			SecComEntity sc = (SecComEntity)page.getRequest().getExtParams().get("sc");
			logger.info("正在解析公司：{"+sc.getAoiId()+":"+sc.getAoiName()+"}");
			sc.setMriChineseName(scObj.getString("MRI_CHINESE_NAME"));
			sc.setMriComWebsite(scObj.getString("MRI_COM_WEBSITE"));
			sc.setMriInfoReg(scObj.getString("MRI_INFO_REG"));
			sc.setMriLegalRepresentative(scObj.getString("MRI_LEGAL_REPRESENTATIVE"));
			String regCapital = scObj.getString("MRI_REG_CAPITAL");
			if(StringUtils.isNotEmpty(regCapital)){
				sc.setMriRegCapital(Double.valueOf(regCapital.replace(",", "")));
			}
			if("SELECT_ZQ_REG_INFO".equals(page.getRequest().getParamsByName("sqlval"))){
				if(StringUtils.isNotEmpty(scObj.getString("MRI_CUSTOMER_SERVICE_TEL"))){
					sc.setMriCustomerServiceTel(scObj.getString("MRI_CUSTOMER_SERVICE_TEL"));
				}
				if(StringUtils.isNotEmpty(scObj.getString("MRI_EMAIL"))){
					sc.setMriEmail(scObj.getString("MRI_EMAIL"));
				}
				if(StringUtils.isNotEmpty(scObj.getString("MRI_LICENSE_CODE"))){
					sc.setMriLicenseCode(scObj.getString("MRI_LICENSE_CODE"));
				}
				if(StringUtils.isNotEmpty(scObj.getString("MRI_OFFICE_ADDRESS"))){
					sc.setMriOfficeAddress(scObj.getString("MRI_OFFICE_ADDRESS"));
				}
				if(StringUtils.isNotEmpty(scObj.getString("MRI_OFFICE_ZIP_CODE"))){
					sc.setMriOfficeZipCode(scObj.getString("MRI_OFFICE_ZIP_CODE"));
				}
			}
			sc.setUrl(TOP_URL+"?aoi_id="+sc.getAoiId()+"&is_org_search=no");
			sc.setId(UuIdPublic.timeStrongID(5));
			StringUtils.removeSpace(sc);
			objectContainer.add(sc);
			int count = atomicInteger.incrementAndGet();
			logger.info("解析第"+count+"条数据完成，当前公司：{"+sc.getAoiId()+":"+sc.getAoiName()+"}");
			
		}else{//个别请求的sqlval的值为SELECT_ZQ_ORM_INFO，具体原因未知
			PageRequest ormReq = context.createPageRequest(SEARCH_URL, SecuritiesCompanyInfoProcessor.class);
			SecComEntity sc = (SecComEntity)page.getRequest().getExtParams().get("sc");
			PageRequest request = page.getRequest();
			ormReq.setMethod(request.getMethod());
			ormReq.setPostType(request.getPostType());
			ormReq.setHeaders(request.getHeaders());
			ormReq.putHeader("Referer", "http://jg.sac.net.cn/pages/publicity/securities_reg.html?aoi_id="+sc.getAoiId()+"&is_org_search=no");
			
			ormReq.putParams("filter_EQS_AOI#AOI_ID", sc.getAoiId());
			ormReq.putParams("sqlkey", "publicity");
			ormReq.putParams("sqlval", "SELECT_ZQ_ORM_INFO");
			
			ormReq.getExtParams().put("sc", sc);
				
			queue.add(ormReq);
		}
		
		
		
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
